<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------
namespace app\shop\model;

use think\Db;
use think\Model;
use app\user\model\UserAddressModel;
use app\admin\model\FreightModel;

class OrderModel extends Model
{
    //获取订单详情
   function get_order(){
       $car = new CarModel();
       $freight= new FreightModel();
       $userAddress = new UserAddressModel();
       $user_id=cmf_get_current_user_id();
       $new_user=Db::name('user')->where('id',$user_id)->find();
       cmf_update_current_user($new_user);
       $user=cmf_get_current_user();
       $is_default=1;
       $address_list=$userAddress->get_address_list($user_id,$is_default);
       $data['address_list']=$address_list;
       $count_post_price = 0;
       $count_post_score = 0;
       $fright=0;
       $car_list = $car->get_car_list($user_id);
       if($car_list==null){
          return 1;
       }
       //购买商品列表
       $data['car_list']=$car_list;
       foreach ($car_list as $key => $val) {
           if ($val['is_checked'] == 1) {
               $count_post_price = $count_post_price + $val['goods']['post_price'] * $val['num'];
               $count_post_score = $count_post_score + $val['goods']['post_score'] * $val['num'];
               //计算运费
               if($data['address_list']!=null){
                   $fright=$fright+$freight->get_good_fright($val['good_id'],$val['num'],$data['address_list'][0]['district']);
               }else{
                   $fright=$fright+$freight->get_good_fright($val['good_id'],$val['num'],0);
               }

           }
       }
       //商品总价格
       $data['count_post_price']=$count_post_price;
       //商品总积分
       $data['count_post_score']=$count_post_score;
       //商品运费
       $data['fright']=$fright;
       $data['count_price']="￥".$count_post_price."+".$count_post_score."积分";
       if($user['score']<$count_post_score){
           $data['message']='<div class="message"><span>您拥有'.$user['score'].'积分,积分不足将使用现金1:1支付</span></div>';
           $data['count_yifu_price']=$count_post_score-$user['score']+$count_post_price+$fright;
           $data['count_yifu_score']=$user['score'];
       }else{
           $data['message']='';
           $data['count_yifu_price']=$count_post_price+$fright;
           $data['count_yifu_score']=$count_post_score;
       }
       return $data;
   }

    /**
     * 关联订单商品表
     */
    public function OrderGoods()
    {
        return $this->hasMany('OrderGoodsModel', 'order_id', 'order_id');
    }
    public function Store()
    {
        return $this->hasOne('StoreModel', 'id', 'shop_id');
    }
    public function User()
    {
        return $this->hasOne('UserModel', 'id', 'buyer_id');
    }
    public function Shipping()
    {
        return $this->hasOne('ShippingModel', 'order_id', 'order_id');
    }
   //获取用户订单列表
    function get_user_order($start=0,$num=10){
        $user_id=cmf_get_current_user_id();
        if($user_id){
            $user_orders=$this->where('buyer_id',$user_id)->order('order_id desc')->with('OrderGoods')->limit($start,$num)->select()->toArray();
            return $user_orders;
        }
    }

    function get_order_detail($id){
        $orders_info=$this->where('order_id',$id)->with('Store')->with('OrderGoods')->with('Shipping')->find()->toArray();
        return $orders_info;
    }

    function get_order_list($param){
        $where=['is_deleted'=>0];
        $param['order_no'] = empty($param['order_no']) ? 0 : $param['order_no'];
        $param['order_status'] = empty($param['order_status']) ? -2 : $param['order_status'];
        if($param['order_no']){
            $where['order_no']=$param['order_no'];
        }
        if($param['order_status']!=-2&&$param['order_status']){
            $where['order_status']=$param['order_status'];
        }
        if($param['order_status']!=-2&&$param['order_status']){
            $where['order_status']=$param['order_status'];
        }
        $startTime = empty($param['start_time']) ? 0 : strtotime($param['start_time']);
        $endTime   = empty($param['end_time']) ? 0 : strtotime($param['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['create_time'] = ['<= time', $endTime];
            }
        }
        $user_orders=$this->where($where)->with('OrderGoods')->with('User')->order('order_id desc')->paginate(10);
        return $user_orders;
    }
}
